<template>
  <el-dialog
    :title="!dataForm.requirementId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="租购房内容" prop="requirementContext">
      <el-input v-model="dataForm.requirementContext" placeholder="租购房内容"></el-input>
    </el-form-item>
    <el-form-item label="意向区域" prop="requirementArea">
      <el-input v-model="dataForm.requirementArea" placeholder="意向区域"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="requirementPrice">
      <el-input v-model="dataForm.requirementPrice" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="requirementUserid">
      <el-input v-model="dataForm.requirementUserid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="省id" prop="requirement
province">
      <el-input v-model="dataForm.requirement
province" placeholder="省id"></el-input>
    </el-form-item>
    <el-form-item label="市id" prop="requirement
city">
      <el-input v-model="dataForm.requirement
city" placeholder="市id"></el-input>
    </el-form-item>
    <el-form-item label="需求类型（租房、买房）" prop="requirementType">
      <el-input v-model="dataForm.requirementType" placeholder="需求类型（租房、买房）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          requirementId: 0,
          requirementContext: '',
          requirementArea: '',
          requirementPrice: '',
          requirementUserid: '',
          requirement
province: '',
          requirement
city: '',
          requirementType: ''
        },
        dataRule: {
          requirementContext: [
            { required: true, message: '租购房内容不能为空', trigger: 'blur' }
          ],
          requirementArea: [
            { required: true, message: '意向区域不能为空', trigger: 'blur' }
          ],
          requirementPrice: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          requirementUserid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          requirement
province: [
            { required: true, message: '省id不能为空', trigger: 'blur' }
          ],
          requirement
city: [
            { required: true, message: '市id不能为空', trigger: 'blur' }
          ],
          requirementType: [
            { required: true, message: '需求类型（租房、买房）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.requirementId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.requirementId) {
            this.$http({
              url: this.$http.adornUrl(`/house/requirement/info/${this.dataForm.requirementId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.requirementContext = data.requirement.requirementContext
                this.dataForm.requirementArea = data.requirement.requirementArea
                this.dataForm.requirementPrice = data.requirement.requirementPrice
                this.dataForm.requirementUserid = data.requirement.requirementUserid
                this.dataForm.requirement
province = data.requirement.requirement
province
                this.dataForm.requirement
city = data.requirement.requirement
city
                this.dataForm.requirementType = data.requirement.requirementType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/requirement/${!this.dataForm.requirementId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'requirementId': this.dataForm.requirementId || undefined,
                'requirementContext': this.dataForm.requirementContext,
                'requirementArea': this.dataForm.requirementArea,
                'requirementPrice': this.dataForm.requirementPrice,
                'requirementUserid': this.dataForm.requirementUserid,
                'requirement
province': this.dataForm.requirement
province,
                'requirement
city': this.dataForm.requirement
city,
                'requirementType': this.dataForm.requirementType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
